/*
 * Copyright 2013 University of Padua, Italy
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


/**
	@author: Coccato Mattia
	@date: 2013-07-31
	@revision: 1.0
**/

package it.unipd.dei.bd.icineasti.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import it.unipd.dei.bd.icineasti.servlet.AbstractDatabaseServlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/** Bozza di classe per l'export delle immagini dal DB. Sostituita da @see ImageServlet. 
 */
public class ShowEventImageServlet extends AbstractDatabaseServlet {
	
	
	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {

		
		//importo per prima cosa l'id dell'evento da modificare
		String idAsString = req.getParameter("idEvento");

		if (idAsString == null) {
			res.getWriter().println("<html><head><title>errore</title></head><body>Non è stato fornito l'idEvento!</body></html>");
			return;
		}

		int idEvento;

		try {
			idEvento = Integer.parseInt(idAsString);
		} catch (NumberFormatException e) {
			res.getWriter().printf("<html><head><title>errore</title></head><body><p><center><font color=\"red\" size=\"+2\">Errore conversione parametro: %s</font></center></p></body></html>",
					e.getMessage());
			return;
		}
		
		
		Connection conn;
		
		try {
			conn = DS.getConnection();
			PreparedStatement st = conn.prepareStatement("SELECT locandina FROM evento WHERE codice = ?");
			st.setInt(1, idEvento);
			ResultSet rs = st.executeQuery();
			while (rs.next()) {
				byte[] image = rs.getBytes("locandina");
				res.setContentLength(image.length);
				res.getOutputStream().write(image);
				
				
				
			}
			conn.close();
		} catch (Exception e) {
			res.getWriter().println(e.getMessage());
		}
	}

}
